package com.aji.biomarker.controller;

import com.aji.allcommon.result.Result;
import com.aji.biomarker.domain.Species;
import com.aji.biomarker.domain.dto.SpeciesDto;
import com.aji.biomarker.domain.dto.SpeciesOriginDto;
import com.aji.biomarker.service.SpeciesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "物种管理相关接口")
@RestController
@RequestMapping("/spe")
@RequiredArgsConstructor
@CrossOrigin
public class SpeciesController {
    private final SpeciesService speciesService;
    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Species entity){
        speciesService.save(entity);
        return Result.success();
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        speciesService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Species entity){
        System.out.println(entity);
        speciesService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(speciesService.getById(id));
    }

    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list() {
        QueryWrapper<Species> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT origin", "remark", "origin_category"); // 按 origin + remark 去重
        return Result.success(speciesService.list(wrapper));
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize,String originCategory, String origin,String remark){
        //合法检验
        if (page < 1 || pageSize < 1) {
            return Result.error("分页参数异常");
        }

        Page<Species> speciesPage = new Page<>(page, pageSize);

        // QueryWrapper 查询 + 去重
        QueryWrapper<Species> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT origin", "remark", "origin_category");
        if(originCategory != null && !"".equals(originCategory)){
            wrapper.like("origin_category",originCategory);
        }
        if (origin != null && !origin.isEmpty()) {
            wrapper.like("origin", origin);
        }
        if (remark != null && !remark.isEmpty()) {
            wrapper.like("remark", remark);
        }

        IPage<Species> speciesIPage = speciesService.page(speciesPage, wrapper);

        return Result.success(speciesIPage);
    }

    @ApiOperation("数据统计")
    @GetMapping("/count")
    public Result<List<SpeciesDto>> getOriginCounts() {
        try {
            List<SpeciesDto> counts = speciesService.countBySpecies();
            return Result.success(counts);
        } catch (Exception e) {
            return Result.error("获取数据统计失败: " + e.getMessage());
        }
    }

    @ApiOperation("具体数据统计")
    @GetMapping("/origin/count")
    public Result<List<SpeciesOriginDto>> getSpeOriginCounts() {
        try {
            List<SpeciesOriginDto> counts = speciesService.countBySpeciesOrigin();
            return Result.success(counts);
        } catch (Exception e) {

            return Result.error("获取数据统计失败: " + e.getMessage());
        }
    }
}
